<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="监测站ID" prop="stationId">
        <el-input
          v-model="queryParams.stationId"
          placeholder="请输入监测站ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeCreateTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['hma:pollutant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hma:pollutant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hma:pollutant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['hma:pollutant:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          @click="router.push({ path: '/hma/pollchart' })"
        >历史趋势图</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pollutantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="readingId" />
      <!-- <el-table-column label="监测站ID" align="center" prop="stationId" /> -->
      <el-table-column label="锑 (Sb) 浓度 (mg/L)" align="center" prop="seleniumSelenium" />
      <el-table-column label="砷 (As) 浓度 (mg/L)" align="center" prop="arsenic" />
      <el-table-column label="铜 (Cu) 浓度 (mg/L)" align="center" prop="copper" />
      <el-table-column label="钡 (Ba) 浓度 (mg/L)" align="center" prop="barium" />
      <el-table-column label="镉 (Cd) 浓度 (mg/L)" align="center" prop="cadmium" />
      <el-table-column label="铬 (Cr) 浓度 (mg/L)" align="center" prop="chromium" />
      <el-table-column label="铅 (Pb) 浓度 (mg/L)" align="center" prop="plumbum" />
      <el-table-column label="汞 (Hg) 浓度 (mg/L)" align="center" prop="mercury" />
      <el-table-column label="锰 (Mn) 浓度 (mg/L)" align="center" prop="manganese" />
      <el-table-column label="镍 (Ni) 浓度 (mg/L)" align="center" prop="nickel" />
      <el-table-column label="银 (Ag) 浓度 (mg/L)" align="center" prop="silver" />
      <el-table-column label="硼酸盐 (以 B 计) 浓度 (mg/L)" align="center" prop="borate" />
      <el-table-column label="硝酸盐 (以 NO3- 计) 浓度 (mg/L)" align="center" prop="nitrate" />
      <el-table-column label="氟化物 (以 F- 计) 浓度 (mg/L)" align="center" prop="fluoride" />
      <el-table-column label="耗氧量 (以 O2 计) 浓度 (mg/L)" align="center" prop="oxygenDemand" />
      <el-table-column label="挥发酚 (以苯酚计) 浓度 (mg/L)" align="center" prop="volatilePhenol" />
      <el-table-column label="氰化物 (以 CN- 计) 浓度 (mg/L)" align="center" prop="cyanide" />
      <el-table-column label="阴离子合成洗涤剂浓度 (mg/L)" align="center" prop="anionicDetergent" />
      <el-table-column label="矿物油浓度 (mg/L)" align="center" prop="mineralOil" />
      <el-table-column label="亚硝酸盐 (以 NO2- 计) 浓度 (mg/L)" align="center" prop="nitrite" />
      <el-table-column label="溴酸盐浓度 (mg/L)" align="center" prop="bromate" />
      <el-table-column label="总放射性浓度 (Bq/L)" align="center" prop="totalRadioactivity" />
      <el-table-column label="226镭放射性浓度 (Bq/L)" align="center" prop="radium226Radioactivity" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['hma:pollutant:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['hma:pollutant:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改污染物监测对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="pollutantRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联到 hma_stations 表中的 station_id" prop="stationId">
          <el-input v-model="form.stationId" placeholder="请输入关联到 hma_stations 表中的 station_id" />
        </el-form-item>
        <el-form-item label="锑 (Sb) 浓度 (mg/L), 限量 &lt; 0.005 mg/L" prop="seleniumSelenium">
          <el-input v-model="form.seleniumSelenium" placeholder="请输入锑 (Sb) 浓度 (mg/L), 限量 &lt; 0.005 mg/L" />
        </el-form-item>
        <el-form-item label="砷 (As) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" prop="arsenic">
          <el-input v-model="form.arsenic" placeholder="请输入砷 (As) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" />
        </el-form-item>
        <el-form-item label="铜 (Cu) 浓度 (mg/L), 限量 &lt; 1.0 mg/L" prop="copper">
          <el-input v-model="form.copper" placeholder="请输入铜 (Cu) 浓度 (mg/L), 限量 &lt; 1.0 mg/L" />
        </el-form-item>
        <el-form-item label="钡 (Ba) 浓度 (mg/L), 限量 &lt; 0.7 mg/L" prop="barium">
          <el-input v-model="form.barium" placeholder="请输入钡 (Ba) 浓度 (mg/L), 限量 &lt; 0.7 mg/L" />
        </el-form-item>
        <el-form-item label="镉 (Cd) 浓度 (mg/L), 限量 &lt; 0.003 mg/L" prop="cadmium">
          <el-input v-model="form.cadmium" placeholder="请输入镉 (Cd) 浓度 (mg/L), 限量 &lt; 0.003 mg/L" />
        </el-form-item>
        <el-form-item label="铬 (Cr) 浓度 (mg/L), 限量 &lt; 0.05 mg/L" prop="chromium">
          <el-input v-model="form.chromium" placeholder="请输入铬 (Cr) 浓度 (mg/L), 限量 &lt; 0.05 mg/L" />
        </el-form-item>
        <el-form-item label="铅 (Pb) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" prop="plumbum">
          <el-input v-model="form.plumbum" placeholder="请输入铅 (Pb) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" />
        </el-form-item>
        <el-form-item label="汞 (Hg) 浓度 (mg/L), 限量 &lt; 0.001 mg/L" prop="mercury">
          <el-input v-model="form.mercury" placeholder="请输入汞 (Hg) 浓度 (mg/L), 限量 &lt; 0.001 mg/L" />
        </el-form-item>
        <el-form-item label="锰 (Mn) 浓度 (mg/L), 限量 &lt; 0.4 mg/L" prop="manganese">
          <el-input v-model="form.manganese" placeholder="请输入锰 (Mn) 浓度 (mg/L), 限量 &lt; 0.4 mg/L" />
        </el-form-item>
        <el-form-item label="镍 (Ni) 浓度 (mg/L), 限量 &lt; 0.02 mg/L" prop="nickel">
          <el-input v-model="form.nickel" placeholder="请输入镍 (Ni) 浓度 (mg/L), 限量 &lt; 0.02 mg/L" />
        </el-form-item>
        <el-form-item label="银 (Ag) 浓度 (mg/L), 限量 &lt; 0.05 mg/L" prop="silver">
          <el-input v-model="form.silver" placeholder="请输入银 (Ag) 浓度 (mg/L), 限量 &lt; 0.05 mg/L" />
        </el-form-item>
        <el-form-item label="硼酸盐 (以 B 计) 浓度 (mg/L), 限量 &lt; 5 mg/L" prop="borate">
          <el-input v-model="form.borate" placeholder="请输入硼酸盐 (以 B 计) 浓度 (mg/L), 限量 &lt; 5 mg/L" />
        </el-form-item>
        <el-form-item label="硝酸盐 (以 NO3- 计) 浓度 (mg/L), 限量 &lt; 45 mg/L" prop="nitrate">
          <el-input v-model="form.nitrate" placeholder="请输入硝酸盐 (以 NO3- 计) 浓度 (mg/L), 限量 &lt; 45 mg/L" />
        </el-form-item>
        <el-form-item label="氟化物 (以 F- 计) 浓度 (mg/L), 限量 &lt; 1.5 mg/L" prop="fluoride">
          <el-input v-model="form.fluoride" placeholder="请输入氟化物 (以 F- 计) 浓度 (mg/L), 限量 &lt; 1.5 mg/L" />
        </el-form-item>
        <el-form-item label="耗氧量 (以 O2 计) 浓度 (mg/L), 限量 &lt; 3.0 mg/L" prop="oxygenDemand">
          <el-input v-model="form.oxygenDemand" placeholder="请输入耗氧量 (以 O2 计) 浓度 (mg/L), 限量 &lt; 3.0 mg/L" />
        </el-form-item>
        <el-form-item label="挥发酚 (以苯酚计) 浓度 (mg/L), 限量 &lt; 0.002 mg/L" prop="volatilePhenol">
          <el-input v-model="form.volatilePhenol" placeholder="请输入挥发酚 (以苯酚计) 浓度 (mg/L), 限量 &lt; 0.002 mg/L" />
        </el-form-item>
        <el-form-item label="氰化物 (以 CN- 计) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" prop="cyanide">
          <el-input v-model="form.cyanide" placeholder="请输入氰化物 (以 CN- 计) 浓度 (mg/L), 限量 &lt; 0.01 mg/L" />
        </el-form-item>
        <el-form-item label="阴离子合成洗涤剂浓度 (mg/L), 限量 &lt; 0.3 mg/L" prop="anionicDetergent">
          <el-input v-model="form.anionicDetergent" placeholder="请输入阴离子合成洗涤剂浓度 (mg/L), 限量 &lt; 0.3 mg/L" />
        </el-form-item>
        <el-form-item label="矿物油浓度 (mg/L), 限量 &lt; 0.05 mg/L" prop="mineralOil">
          <el-input v-model="form.mineralOil" placeholder="请输入矿物油浓度 (mg/L), 限量 &lt; 0.05 mg/L" />
        </el-form-item>
        <el-form-item label="亚硝酸盐 (以 NO2- 计) 浓度 (mg/L), 限量 &lt; 0.1 mg/L" prop="nitrite">
          <el-input v-model="form.nitrite" placeholder="请输入亚硝酸盐 (以 NO2- 计) 浓度 (mg/L), 限量 &lt; 0.1 mg/L" />
        </el-form-item>
        <el-form-item label="溴酸盐浓度 (mg/L), 限量 &lt; 0.01 mg/L" prop="bromate">
          <el-input v-model="form.bromate" placeholder="请输入溴酸盐浓度 (mg/L), 限量 &lt; 0.01 mg/L" />
        </el-form-item>
        <el-form-item label="总放射性浓度 (Bq/L), 限量 &lt; 1.5 Bq/L" prop="totalRadioactivity">
          <el-input v-model="form.totalRadioactivity" placeholder="请输入总放射性浓度 (Bq/L), 限量 &lt; 1.5 Bq/L" />
        </el-form-item>
        <el-form-item label="226镭放射性浓度 (Bq/L), 限量 &lt; 1.1 Bq/L" prop="radium226Radioactivity">
          <el-input v-model="form.radium226Radioactivity" placeholder="请输入226镭放射性浓度 (Bq/L), 限量 &lt; 1.1 Bq/L" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Pollutant">
import { listPollutant, getPollutant, delPollutant, addPollutant, updatePollutant } from "@/api/hma/pollutant";
import { useRouter } from 'vue-router';
const router = useRouter();

const { proxy } = getCurrentInstance();

const pollutantList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    stationId: null,
    createTime: null,
  },
  rules: {
    stationId: [
      { required: true, message: "关联到 hma_stations 表中的 station_id不能为空", trigger: "blur" }
    ],
    seleniumSelenium: [
      { required: true, message: "锑 (Sb) 浓度 (mg/L), 限量 &lt; 0.005 mg/L不能为空", trigger: "blur" }
    ],
    arsenic: [
      { required: true, message: "砷 (As) 浓度 (mg/L), 限量 &lt; 0.01 mg/L不能为空", trigger: "blur" }
    ],
    copper: [
      { required: true, message: "铜 (Cu) 浓度 (mg/L), 限量 &lt; 1.0 mg/L不能为空", trigger: "blur" }
    ],
    barium: [
      { required: true, message: "钡 (Ba) 浓度 (mg/L), 限量 &lt; 0.7 mg/L不能为空", trigger: "blur" }
    ],
    cadmium: [
      { required: true, message: "镉 (Cd) 浓度 (mg/L), 限量 &lt; 0.003 mg/L不能为空", trigger: "blur" }
    ],
    chromium: [
      { required: true, message: "铬 (Cr) 浓度 (mg/L), 限量 &lt; 0.05 mg/L不能为空", trigger: "blur" }
    ],
    plumbum: [
      { required: true, message: "铅 (Pb) 浓度 (mg/L), 限量 &lt; 0.01 mg/L不能为空", trigger: "blur" }
    ],
    mercury: [
      { required: true, message: "汞 (Hg) 浓度 (mg/L), 限量 &lt; 0.001 mg/L不能为空", trigger: "blur" }
    ],
    manganese: [
      { required: true, message: "锰 (Mn) 浓度 (mg/L), 限量 &lt; 0.4 mg/L不能为空", trigger: "blur" }
    ],
    nickel: [
      { required: true, message: "镍 (Ni) 浓度 (mg/L), 限量 &lt; 0.02 mg/L不能为空", trigger: "blur" }
    ],
    silver: [
      { required: true, message: "银 (Ag) 浓度 (mg/L), 限量 &lt; 0.05 mg/L不能为空", trigger: "blur" }
    ],
    borate: [
      { required: true, message: "硼酸盐 (以 B 计) 浓度 (mg/L), 限量 &lt; 5 mg/L不能为空", trigger: "blur" }
    ],
    nitrate: [
      { required: true, message: "硝酸盐 (以 NO3- 计) 浓度 (mg/L), 限量 &lt; 45 mg/L不能为空", trigger: "blur" }
    ],
    fluoride: [
      { required: true, message: "氟化物 (以 F- 计) 浓度 (mg/L), 限量 &lt; 1.5 mg/L不能为空", trigger: "blur" }
    ],
    oxygenDemand: [
      { required: true, message: "耗氧量 (以 O2 计) 浓度 (mg/L), 限量 &lt; 3.0 mg/L不能为空", trigger: "blur" }
    ],
    volatilePhenol: [
      { required: true, message: "挥发酚 (以苯酚计) 浓度 (mg/L), 限量 &lt; 0.002 mg/L不能为空", trigger: "blur" }
    ],
    cyanide: [
      { required: true, message: "氰化物 (以 CN- 计) 浓度 (mg/L), 限量 &lt; 0.01 mg/L不能为空", trigger: "blur" }
    ],
    anionicDetergent: [
      { required: true, message: "阴离子合成洗涤剂浓度 (mg/L), 限量 &lt; 0.3 mg/L不能为空", trigger: "blur" }
    ],
    mineralOil: [
      { required: true, message: "矿物油浓度 (mg/L), 限量 &lt; 0.05 mg/L不能为空", trigger: "blur" }
    ],
    nitrite: [
      { required: true, message: "亚硝酸盐 (以 NO2- 计) 浓度 (mg/L), 限量 &lt; 0.1 mg/L不能为空", trigger: "blur" }
    ],
    bromate: [
      { required: true, message: "溴酸盐浓度 (mg/L), 限量 &lt; 0.01 mg/L不能为空", trigger: "blur" }
    ],
    totalRadioactivity: [
      { required: true, message: "总放射性浓度 (Bq/L), 限量 &lt; 1.5 Bq/L不能为空", trigger: "blur" }
    ],
    radium226Radioactivity: [
      { required: true, message: "226镭放射性浓度 (Bq/L), 限量 &lt; 1.1 Bq/L不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    createBy: [
      { required: true, message: "创建人不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询污染物监测列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  listPollutant(queryParams.value).then(response => {
    pollutantList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    readingId: null,
    stationId: null,
    seleniumSelenium: null,
    arsenic: null,
    copper: null,
    barium: null,
    cadmium: null,
    chromium: null,
    plumbum: null,
    mercury: null,
    manganese: null,
    nickel: null,
    silver: null,
    borate: null,
    nitrate: null,
    fluoride: null,
    oxygenDemand: null,
    volatilePhenol: null,
    cyanide: null,
    anionicDetergent: null,
    mineralOil: null,
    nitrite: null,
    bromate: null,
    totalRadioactivity: null,
    radium226Radioactivity: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("pollutantRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.readingId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加污染物监测";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _readingId = row.readingId || ids.value
  getPollutant(_readingId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改污染物监测";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pollutantRef"].validate(valid => {
    if (valid) {
      if (form.value.readingId != null) {
        updatePollutant(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPollutant(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _readingIds = row.readingId || ids.value;
  proxy.$modal.confirm('是否确认删除污染物监测编号为"' + _readingIds + '"的数据项？').then(function() {
    return delPollutant(_readingIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('hma/pollutant/export', {
    ...queryParams.value
  }, `pollutant_${new Date().getTime()}.xlsx`)
}

getList();
</script>
